﻿using QFramework;
using UnityEngine;
using System;
using System.Collections;

namespace HareGame
{
    public class GameManager : MonoBehaviour, ISingleton
    {
        private EventID EventID = new EventID();
        public int EventLength { get; private set; }

        /// <summary>
        /// 初始化
        /// </summary>
        public void OnSingletonInit()
        {
            EventLength = Enum.GetNames(EventID.GetType()).Length;
        }

        /// <summary>
        /// 定义单例
        /// </summary>
        public static GameManager Instance
        {
            get { return MonoSingletonProperty<GameManager>.Instance; }
        }

        public void Launcher()
        {
            LogKit.I("游戏开始");
        }

        void OnApplicationPause(bool pause)
        {
            if (pause)
            {
                double oadate = DateTime.Now.ToOADate();
                GameEntry.GameData.SetDouble(UserData.OfflineDate, oadate);
                StopCoroutine("ResetOfflineTime");
            }
            else
            {
                SetOfflineDate();
            }
        }

        public void SetOfflineDate()
        {
            TimeSpan ts = DateTime.Now - DateTime.FromOADate(GameEntry.GameData.GetDouble(UserData.OfflineDate, DateTime.Now.ToOADate()));
            LogKit.I("本次离线时长:<color=green>" + ts.TotalSeconds.ToString("F0") + "秒</color>");
            StartCoroutine("ResetOfflineTime");
        }

        IEnumerator ResetOfflineTime()
        {
            double oadate = DateTime.Now.ToOADate();
            GameEntry.GameData.SetDouble(UserData.OfflineDate, oadate);
            yield return new WaitForSeconds(60);
            StartCoroutine("ResetOfflineTime");
        }
    }
}